#coding=utf-8

import uuid
import hashlib
from controller.controller_base import *


class UserController(ControllerBase):

    def __init__(self):
        super(UserController, self).__init__()

    def login(self, username, password):
        result = {}
        result['username'] = username
        result['password'] = password

        # 从数据库查询用户信息，如果正确，产生一个uuid，返回
        user_item = self.db.session.query(User).filter(User.username == username,
                                                       User.password == hashlib.md5(password).hexdigest()).first()
        if user_item:
            token = user_item.token or uuid.uuid1().get_hex()
            user_item.token = token
            result['token'] = token

        self.db.session.commit()

        return result
